package api

import (
	"FeedXmh/app/dao"
	"fmt"
	jwt "github.com/gogf/gf-jwt"
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

var Permission = permission{}

type permission struct {}

func (* permission)GET(r *ghttp.Request) {
	where:=" 1=1 "
	role_id := r.GetString("role_id")
	if role_id!=""{
		where+=fmt.Sprintf(" and role_id='%s'",role_id)
	}
	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path
	db:=g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url,r.Method, userid) {
		res, err := db.Model("v_role_permission").Where(where).Select()
		if err != nil {
			g.Log().Line().Error(err)
		}
		r.Response.WriteJson(g.Map{
			"code": 0,
			"msg":  "",
			"data": res.List(),
		})
	} else {
		r.Response.WriteJson(g.Map{
			"code": 0,
			"msg":  "permission denied",
			"data": nil,
		})
	}

	r.ExitAll()
}

func (* permission)PUT(r *ghttp.Request) {
	res := g.Map{
		"code": -1,
		"msg":  "保存失败",
	}
	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path
	params := r.GetFormMap()
	id := params["id"]
	permission := r.GetString("permission")
	permission_json, err := gjson.Decode(permission)
	if err != nil {
		res = g.Map{
			"code": -1,
			"msg":  err.Error(),
		}
	} else {
		db:=g.DB(g.Cfg().GetString("custom.db_name"))
		if dao.CheckApiPermission(url,r.Method, userid) {
			if tx, err := db.Begin(); err == nil {
				//情况permission表
				_, err = tx.Model("t_permission").Where("role_id=?", id).Delete()
				if err == nil {
					//增加permission
					_, err = tx.Model("t_permission").Data(permission_json).Insert()
					if err == nil {
						res["code"] = 1
						res["msg"] = "保存成功"
					}else{
						res["code"] = -1
						res["msg"] = err.Error()
					}
				}
				if res["code"].(int) >= 0 {
					tx.Commit()
				} else {
					tx.Rollback()
				}
			}
		} else {
			res["code"] = -1
			res["msg"] = "Permission denied"
		}
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (* permission)POST(r *ghttp.Request) {
	res := g.Map{
		"code": -1,
		"msg":  "保存失败",
	}
	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path

	params := r.GetFormMap()
	id := params["id"]
	//id := r.GetString("id")

	permission := r.GetString("permission")
	permission_json, err := gjson.Decode(permission)
	if err != nil {
		res = g.Map{
			"code": -1,
			"msg":  err.Error(),
		}
	} else {
		db:=g.DB(g.Cfg().GetString("custom.db_name"))
		if dao.CheckApiPermission(url,r.Method, userid) {
			if tx, err := db.Begin(); err == nil {
				//情况permission表
				_, err = tx.Model("t_permission").Where("role_id=?", id).Delete()
				if err == nil {
					//增加permission
					_, err = tx.Model("t_permission").Data(permission_json).Insert()
					if err == nil {
						res["code"] = 1
						res["msg"] = "保存成功"
					}else{
						res["code"] = -1
						res["msg"] = err.Error()
					}
				}
				if res["code"].(int) >= 0 {
					tx.Commit()
				} else {
					tx.Rollback()
				}
			}
		} else {
			res["code"] = -1
			res["msg"] = "Permission denied"
		}
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}

func (* permission)DELETE(r *ghttp.Request) {
	res := g.Map{
		"code": 0,
		"msg":  "",
	}
	params := r.GetFormMap()
	id := params["id"]

	//id := r.GetString("id")
	claims := jwt.ExtractClaims(r)
	current_user := claims["userid"].(string)
	url := r.URL.Path
	db:=g.DB(g.Cfg().GetString("custom.db_name"))
	if dao.CheckApiPermission(url,r.Method, current_user) {
		sqlres, _ := db.Model("t_role").Where("id=?", id).Delete()
		if a, err := sqlres.RowsAffected(); err == nil && a >= 0 {
			res["code"] = 1
			res["msg"] = "保存成功"
		} else {
			res["code"] = -1
			res["msg"] = "保存失败"
		}
	} else {
		res["code"] = -1
		res["msg"] = "Permission denied"
	}
	r.Response.WriteJson(res)
	r.ExitAll()
}
